package com.cdp.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysStoneSize;
import com.cdp.common.core.domain.entity.SysWaterReducer;
import com.cdp.common.core.domain.entity.SysWaterReducerType;
import com.cdp.system.dto.StoneGrainDTO;
import com.cdp.system.dto.WaterReducerDTO;
import com.cdp.system.dto.WaterReducerSecondDTO;
import com.cdp.system.mapper.SysWaterReducerMapper;
import com.cdp.system.mapper.SysWaterReducerTypeMapper;
import com.cdp.system.service.ISysWaterReducerService;
import com.cdp.system.vo.AdditiveWaterReducerSecondVO;
import com.cdp.system.vo.AdditiveWaterReducerVO;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.WaterReducerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysWaterReducerServiceImpl extends ServiceImpl<SysWaterReducerMapper, SysWaterReducer> implements ISysWaterReducerService {

    @Autowired
    private SysWaterReducerMapper sysWaterReducerMapper;

    @Autowired
    private SysWaterReducerTypeMapper sysWaterReducerTypeMapper;

    /**
     * 查询减水剂
     *
     * @return
     */
    @Override
    public List<SysWaterReducer> listWaterReduce() {
        List<SysWaterReducer> list = list();
        return list;
    }

    @Override
    public Map<String, Object> getwaterReducerAndMax() {
        return sysWaterReducerMapper.getwaterReducerAndMax();
    }


    /**
     * 减水剂型号模糊查询
     *
     * @param stoneGrainDTO
     * @return
     */
    @Override
    public List<SysWaterReducerType> listWaterMistedReduce(StoneGrainDTO stoneGrainDTO) {

        LambdaQueryWrapper<SysWaterReducerType> wrapper = new LambdaQueryWrapper<>();
        // 如果传入的 name 不为空
        if (stoneGrainDTO.getName() != null && !stoneGrainDTO.getName().isEmpty()) {
            wrapper.like(SysWaterReducerType::getName, stoneGrainDTO.getName());
        }

        // 执行查询
        return sysWaterReducerTypeMapper.selectList(wrapper);

    }


    /**
     * 原材料
     * 减水剂输入框模糊查询
     *
     * @param waterReducerDTO
     * @return
     */

    @Override
    public List<WaterReducerVO> listMistedWaterReduce(WaterReducerDTO waterReducerDTO) {
        // 从数据库获取水减剂实体列表
        List<SysWaterReducer> reducerList = sysWaterReducerMapper.listMistedWaterReduce(waterReducerDTO);

        // 创建 VO 列表
        List<WaterReducerVO> waterReducerVOList = new ArrayList<>();
        // 使用 BeanUtils.copyProperties 简化属性赋值
        for (SysWaterReducer reducer : reducerList) {
            WaterReducerVO waterReducerVO = new WaterReducerVO();
            // 复制属性
            BeanUtils.copyProperties(reducer, waterReducerVO);
            // 由于 regionNameStr 是 SysWaterReducer 中没有的字段（通过 @TableField(exist = false) 表示不在数据库中），
            // 所以需要手动设置
            waterReducerVO.setRegionNameStr(reducer.getRegionNameStr());
            // 将 VO 对象加入列表
            waterReducerVOList.add(waterReducerVO);
        }
        // 返回转换后的 VO 列表
        return waterReducerVOList;
    }

    /**
     * 外加剂
     * 减水剂模糊查询
     *
     * @param waterReducerDTO
     * @return
     */
    @Override
    public List<AdditiveWaterReducerVO> listAdditiveMistedWaterReduce(WaterReducerDTO waterReducerDTO) {

        // 从数据库获取水减剂实体列表
        List<SysWaterReducer> reducerList = sysWaterReducerMapper.listMistedWaterReduce(waterReducerDTO);
        ArrayList<AdditiveWaterReducerVO> arrayList = new ArrayList<>();
        for (SysWaterReducer sysWaterReducer : reducerList) {
            AdditiveWaterReducerVO waterReducerVO = new AdditiveWaterReducerVO();
            BeanUtils.copyProperties(sysWaterReducer, waterReducerVO); //第二个是目标
            arrayList.add(waterReducerVO);
            //waterReducerVO.setRegionNameStr(sysWaterReducer.getRegionNameStr());
        }

        return arrayList;
    }


    @Override
    public List<IdsVO> queryWaterReducerIdsById(Integer id) {
        LambdaQueryWrapper<SysWaterReducer> wrapper = new LambdaQueryWrapper<>();
        // 如果 id 不为 null，则进行模糊查询；否则查询全部
        if (id != null) {
            wrapper.like(SysWaterReducer::getReducerId, id);
        }
        List<SysWaterReducer> sysWaterReducerList = sysWaterReducerMapper.selectList(wrapper);
        IdsVO idsVO = new IdsVO();
        List<Integer> sandIds = sysWaterReducerList.stream()
                .map(sysWaterReducer -> sysWaterReducer.getReducerId().intValue()) // 手动转换 Long -> Integer
                .collect(Collectors.toList());
        idsVO.setIds(sandIds);

        return Collections.singletonList(idsVO);
    }



    @Override
    public List<SysWaterReducer> listWaterReduceIds(List<Integer> waterReducerIds) {
        if (waterReducerIds == null || waterReducerIds.isEmpty()) {
            return Collections.emptyList();
        }



        return  sysWaterReducerMapper.listWaterReduceIds(waterReducerIds);
    }


    /**
     * 减水剂模糊查询
     * @param waterReducerSecondDTO
     * @return
     */
    @Override
    public List<AdditiveWaterReducerSecondVO> listAdditiveMistedWaterReduceSeconed(WaterReducerSecondDTO waterReducerSecondDTO) {


        // 从数据库获取水减剂实体列表
        List<SysWaterReducer> reducerList = sysWaterReducerMapper.listAdditiveMistedWaterReduceSeconed(waterReducerSecondDTO);


        ArrayList<AdditiveWaterReducerSecondVO> arrayList = new ArrayList<>();
        for (SysWaterReducer sysWaterReducer : reducerList) {

            AdditiveWaterReducerSecondVO waterReducerVO = new AdditiveWaterReducerSecondVO();
            BeanUtils.copyProperties(sysWaterReducer, waterReducerVO); //第二个是目标
            arrayList.add(waterReducerVO);
            //waterReducerVO.setRegionNameStr(sysWaterReducer.getRegionNameStr());


        }

        return arrayList;
    }


}








